package com.bzframework.project.picture.controller;

import com.bzframework.pro.authorize.adapter.ReactiveServiceCrudControllerAdapter;
import com.bzframework.pro.authorize.annotation.Authorization;
import com.bzframework.pro.authorize.context.ReactiveUserTokenContextHolder;
import com.bzframework.pro.authorize.token.UserToken;
import com.bzframework.project.picture.entity.PictureEntity;
import com.bzframework.project.picture.model.request.PictureReviewRequest;
import com.bzframework.project.picture.service.PictureService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.URL;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.crud.service.ReactiveCrudService;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * @Author bingzhong
 * @Date 2025/1/15 17:15
 * @Description
 */
@Tag(name = "图片管理")
@RestController
@Validated
@RequestMapping("/picture")
@RequiredArgsConstructor
public class PictureController implements ReactiveServiceCrudControllerAdapter<PictureEntity, String> {

    private final PictureService pictureService;

    @Override
    public ReactiveCrudService<PictureEntity, String> getService() {
        return pictureService;
    }

    @PostMapping("/upload")
    @Operation(summary = "上传图片")
    @Authorization(ignore = true)
    public Mono<PictureEntity> uploadPicture(@RequestPart("file") FilePart filePart, PictureEntity picture) {
        return ReactiveUserTokenContextHolder.get()
                .flatMap(userToken -> pictureService.uploadPicture(filePart, picture, userToken));
    }

    @PostMapping("/review")
    @Operation(summary = "审核图片")
    @Authorization(admin = true)
    public Mono<SaveResult> reviewingPicture(@RequestBody PictureReviewRequest request) {
        return ReactiveUserTokenContextHolder.get()
                .flatMap(userToken -> pictureService.doReviewPicture(request, userToken));
    }

    @GetMapping("/upload/url")
    @Operation(summary = "根据文件路径上传图片")
    @Authorization(ignore = true)
    public Mono<Boolean> uploadPictureByUrl(@RequestParam @URL String fileUrl) {
        return ReactiveUserTokenContextHolder.get()
                .defaultIfEmpty(UserToken.of(null,null,null,null))
                .flatMap(userToken -> pictureService.uploadPictureByUrl(userToken, fileUrl));
    }


}
